///////////////////////////////////////////////////////////////////////////////
// extractor.hpp
//
//  Copyright 2005 Eric Niebler. Distributed under the Boost
//  Software License, Version 1.0. (See accompanying file
//  LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

#ifndef AUTOBOOST_ACCUMULATORS_FRAMEWORK_EXTRACTOR_HPP_EAN_28_10_2005
#define AUTOBOOST_ACCUMULATORS_FRAMEWORK_EXTRACTOR_HPP_EAN_28_10_2005

#include <autoboost/preprocessor/tuple/rem.hpp>
#include <autoboost/preprocessor/array/size.hpp>
#include <autoboost/preprocessor/array/data.hpp>
#include <autoboost/preprocessor/array/elem.hpp>
#include <autoboost/preprocessor/seq/to_array.hpp>
#include <autoboost/preprocessor/seq/transform.hpp>
#include <autoboost/preprocessor/repetition/enum_params.hpp>
#include <autoboost/preprocessor/repetition/enum_trailing_params.hpp>
#include <autoboost/preprocessor/repetition/enum_trailing_binary_params.hpp>
#include <autoboost/parameter/binding.hpp>
#include <autoboost/mpl/apply.hpp>
#include <autoboost/mpl/eval_if.hpp>
#include <autoboost/type_traits/remove_reference.hpp>
#include <autoboost/accumulators/accumulators_fwd.hpp>
#include <autoboost/accumulators/framework/parameters/accumulator.hpp>

namespace autoboost { namespace accumulators
{

namespace detail
{
    template<typename AccumulatorSet, typename Feature>
    struct accumulator_set_result
    {
        typedef typename as_feature<Feature>::type feature_type;
        typedef typename mpl::apply<AccumulatorSet, feature_type>::type::result_type type;
    };

    template<typename Args, typename Feature>
    struct argument_pack_result
      : accumulator_set_result<
            typename remove_reference<
                typename parameter::binding<Args, tag::accumulator>::type
            >::type
          , Feature
        >
    {
    };

    template<typename A, typename Feature>
    struct extractor_result
      : mpl::eval_if<
            detail::is_accumulator_set<A>
          , accumulator_set_result<A, Feature>
          , argument_pack_result<A, Feature>
        >
    {
    };

    template<typename Feature, typename AccumulatorSet>
    typename extractor_result<AccumulatorSet, Feature>::type
    do_extract(AccumulatorSet const &acc, mpl::true_)
    {
        typedef typename as_feature<Feature>::type feature_type;
        return extract_result<feature_type>(acc);
    }

    template<typename Feature, typename Args>
    typename extractor_result<Args, Feature>::type
    do_extract(Args const &args, mpl::false_)
    {
        typedef typename as_feature<Feature>::type feature_type;
        return find_accumulator<feature_type>(args[accumulator]).result(args);
    }

} // namespace detail


///////////////////////////////////////////////////////////////////////////////
/// Extracts the result associated with Feature from the specified accumulator_set.
template<typename Feature>
struct extractor
{
    typedef extractor<Feature> this_type;

    /// The result meta-function for determining the return type of the extractor
    template<typename F>
    struct result;

    template<typename A1>
    struct result<this_type(A1)>
      : detail::extractor_result<A1, Feature>
    {
    };

    /// Extract the result associated with Feature from the accumulator set
    /// \param acc The accumulator set object from which to extract the result
    template<typename Arg1>
    typename detail::extractor_result<Arg1, Feature>::type
    operator ()(Arg1 const &arg1) const
    {
        // Arg1 could be an accumulator_set or an argument pack containing
        // an accumulator_set. Dispatch accordingly.
        return detail::do_extract<Feature>(arg1, detail::is_accumulator_set<Arg1>());
    }

    /// \overload
    ///
    /// \param a1 Optional named parameter to be passed to the accumulator's result() function.
    template<typename AccumulatorSet, typename A1>
    typename detail::extractor_result<AccumulatorSet, Feature>::type
    operator ()(AccumulatorSet const &acc, A1 const &a1) const
    {
        AUTOBOOST_MPL_ASSERT((detail::is_accumulator_set<AccumulatorSet>));
        typedef typename as_feature<Feature>::type feature_type;
        return extract_result<feature_type>(acc, a1);
    }

    // ... other overloads generated by Boost.Preprocessor:

    /// INTERNAL ONLY
    ///
#define AUTOBOOST_ACCUMULATORS_EXTRACTOR_FUN_OP(z, n, _)                                    \
    template<AUTOBOOST_PP_ENUM_PARAMS_Z(z, n, typename A)>                                  \
    struct result<this_type(AUTOBOOST_PP_ENUM_PARAMS_Z(z, n, A))>                           \
      : detail::extractor_result<A1, Feature>                                           \
    {};                                                                                 \
    template<                                                                           \
        typename AccumulatorSet                                                         \
        AUTOBOOST_PP_ENUM_TRAILING_PARAMS_Z(z, n, typename A)                               \
    >                                                                                   \
    typename detail::extractor_result<AccumulatorSet, Feature>::type                    \
    operator ()(                                                                        \
        AccumulatorSet const &acc                                                       \
        AUTOBOOST_PP_ENUM_TRAILING_BINARY_PARAMS_Z(z, n, A, const &a)                       \
    ) const                                                                             \
    {                                                                                   \
        AUTOBOOST_MPL_ASSERT((detail::is_accumulator_set<AccumulatorSet>));                 \
        typedef typename as_feature<Feature>::type feature_type;                        \
        return extract_result<feature_type>(acc AUTOBOOST_PP_ENUM_TRAILING_PARAMS_Z(z, n, a));\
    }

    AUTOBOOST_PP_REPEAT_FROM_TO(
        2
      , AUTOBOOST_PP_INC(AUTOBOOST_ACCUMULATORS_MAX_ARGS)
      , AUTOBOOST_ACCUMULATORS_EXTRACTOR_FUN_OP
      , _
    )

    #ifdef AUTOBOOST_ACCUMULATORS_DOXYGEN_INVOKED
    /// \overload
    ///
    template<typename AccumulatorSet, typename A1, typename A2, ...>
    typename detail::extractor_result<AccumulatorSet, Feature>::type
    operator ()(AccumulatorSet const &acc, A1 const &a1, A2 const &a2, ...);
    #endif
};

/// INTERNAL ONLY
///
#define AUTOBOOST_ACCUMULATORS_ARRAY_REM(Array)                                                         \
    AUTOBOOST_PP_TUPLE_REM_CTOR(AUTOBOOST_PP_ARRAY_SIZE(Array), AUTOBOOST_PP_ARRAY_DATA(Array))

/// INTERNAL ONLY
///
#define AUTOBOOST_ACCUMULATORS_SEQ_REM(Seq)                                                             \
    AUTOBOOST_ACCUMULATORS_ARRAY_REM(AUTOBOOST_PP_SEQ_TO_ARRAY(Seq))

/// INTERNAL ONLY
///
#define AUTOBOOST_ACCUMULATORS_ARGS_OP(s, data, elem)                                                   \
    T ## s

/// INTERNAL ONLY
///
#define AUTOBOOST_ACCUMULATORS_PARAMS_OP(s, data, elem)                                                 \
    elem T ## s

/// INTERNAL ONLY
///
#define AUTOBOOST_ACCUMULATORS_MAKE_FEATURE(Tag, Feature, ParamsSeq)                                    \
    Tag::Feature<                                                                                   \
        AUTOBOOST_ACCUMULATORS_SEQ_REM(                                                                 \
            AUTOBOOST_PP_SEQ_TRANSFORM(AUTOBOOST_ACCUMULATORS_ARGS_OP, ~, ParamsSeq)                        \
        )                                                                                           \
    >

/// INTERNAL ONLY
///
#define AUTOBOOST_ACCUMULATORS_DEFINE_EXTRACTOR_FUN_IMPL(z, n, Tag, Feature, ParamsSeq)                 \
    template<                                                                                       \
        AUTOBOOST_ACCUMULATORS_SEQ_REM(                                                                 \
            AUTOBOOST_PP_SEQ_TRANSFORM(AUTOBOOST_ACCUMULATORS_PARAMS_OP, ~, ParamsSeq)                      \
        )                                                                                           \
      , typename Arg1                                                                               \
        AUTOBOOST_PP_ENUM_TRAILING_PARAMS_Z(z, n, typename A)                                           \
    >                                                                                               \
    typename autoboost::accumulators::detail::extractor_result<                                         \
        Arg1                                                                                        \
      , AUTOBOOST_ACCUMULATORS_MAKE_FEATURE(Tag, Feature, ParamsSeq)                                    \
    >::type                                                                                         \
    Feature(Arg1 const &arg1 AUTOBOOST_PP_ENUM_TRAILING_BINARY_PARAMS_Z(z, n, A, const &a) )            \
    {                                                                                               \
        typedef AUTOBOOST_ACCUMULATORS_MAKE_FEATURE(Tag, Feature, ParamsSeq) feature_type;              \
        return autoboost::accumulators::extractor<feature_type>()(                                      \
            arg1 AUTOBOOST_PP_ENUM_TRAILING_PARAMS_Z(z, n, a));                                         \
    }

/// INTERNAL ONLY
///
#define AUTOBOOST_ACCUMULATORS_DEFINE_EXTRACTOR_FUN(z, n, _)                                            \
    AUTOBOOST_ACCUMULATORS_DEFINE_EXTRACTOR_FUN_IMPL(                                                   \
        z                                                                                           \
      , n                                                                                           \
      , AUTOBOOST_PP_ARRAY_ELEM(0, _)                                                                   \
      , AUTOBOOST_PP_ARRAY_ELEM(1, _)                                                                   \
      , AUTOBOOST_PP_ARRAY_ELEM(2, _)                                                                   \
    )

#define AUTOBOOST_ACCUMULATORS_DEFINE_EXTRACTOR(Tag, Feature, ParamSeq)                                 \
    AUTOBOOST_PP_REPEAT(                                                                                \
        AUTOBOOST_PP_INC(AUTOBOOST_ACCUMULATORS_MAX_ARGS)                                                   \
      , AUTOBOOST_ACCUMULATORS_DEFINE_EXTRACTOR_FUN                                                     \
      , (3, (Tag, Feature, ParamSeq))                                                               \
    )

}} // namespace autoboost::accumulators

#endif
